package com.sskj.qiangjin.ui.activity;

import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.common.base.App;
import com.sskj.common.util.CheckUtil;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.presenter.RegisterPresenter;
import com.sskj.qiangjin.util.NumLetterUtil;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * Created by QinKang on 2018/12/28.
 */

@Route(path = RConfig.APP_REGISTER)//注册
public class RegisterActivity extends BaseActivity<RegisterPresenter> {
    @BindView(R.id.et_nickname)//昵称
            EditText etNickname;
    @BindView(R.id.et_tel)//手机号
            EditText etTel;
    @BindView(R.id.et_verification_code)//验证码
            EditText etVerificationCode;
    @BindView(R.id.tv_get_verification_code)//获取验证码
            TextView tvGetVerificationCode;
    @BindView(R.id.et_pwd)//密码
            EditText etPwd;
    @BindView(R.id.et_pwd_again)//再次密码
            EditText etPwdAgain;
    @BindView(R.id.et_invite_code)//邀请码
            EditText etInviteCode;
    @BindView(R.id.iv_register_agree)//协议
            ImageView ivRegisterAgree;
    @BindView(R.id.tv_register)//注册
            TextView tvRegister;
    @BindView(R.id.tv_register_login)//登录
            TextView tvRegisterLogin;
    private DisposableSubscriber<Long> disposableSubscriber;
    private boolean agreeRegister = true;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_register;
    }

    @Override
    public RegisterPresenter getPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.app_activity_register));

        etNickname.setText(NumLetterUtil.getItemID(12));

        ClickUtil.click(ivRegisterAgree, () -> {
            if (agreeRegister) {
                agreeRegister = false;
                ivRegisterAgree.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.app_icon_unregister_msg));
            } else {
                agreeRegister = true;
                ivRegisterAgree.setImageDrawable(ContextCompat.getDrawable(this, R.mipmap.app_icon_register_msg));
            }
        });
        ClickUtil.click(tvGetVerificationCode, () -> {
            if (TextUtils.isEmpty(etTel.getText().toString())) {
                ToastUtil.showShort("请输入手机号");
                return;
            }
            mPresenter.sendCode(etTel.getText().toString().trim());
        });
        ClickUtil.click(tvRegister, () -> {
            if (TextUtils.isEmpty(etNickname.getText().toString().trim())) {
                ToastUtil.showShort("请输入昵称");
                return;
            }
            if (TextUtils.isEmpty(etTel.getText().toString())) {
                ToastUtil.showShort("请输入手机号");
                return;
            }
            if (!CheckUtil.isMobile(etTel.getText().toString())) {
                ToastUtil.showShort("请输入正确的手机号");
                return;
            }
            if (TextUtils.isEmpty(etVerificationCode.getText().toString())) {
                ToastUtil.showShort("请输入验证码");
                return;
            }
            if (TextUtils.isEmpty(etPwd.getText().toString())) {
                ToastUtil.showShort("请输入密码");
                return;
            }
            if (!CheckUtil.isPayPwd(etPwd.getText().toString()) || !CheckUtil.isPayPwd(etPwdAgain.getText().toString())) {
                ToastUtil.showShort("密码格式错误");
                return;
            }
            if (!etPwd.getText().toString().equals(etPwdAgain.getText().toString())) {
                ToastUtil.showShort("两次密码输入不一致");
                return;
            }
            if (TextUtils.isEmpty(etInviteCode.getText().toString())) {
                ToastUtil.showShort("请输入邀请码");
                return;
            }
            if (!agreeRegister) {
                ToastUtil.showShort("请先阅读并同意注册协议");
                return;
            }
            mPresenter.register(etTel.getText().toString().trim()
                    , etPwd.getText().toString().trim()
                    , etVerificationCode.getText().toString().trim()
                    , TextUtils.isEmpty(etInviteCode.getText().toString()) ? "" : etInviteCode.getText().toString()
                    , TextUtils.isEmpty(etNickname.getText().toString()) ? "" : etNickname.getText().toString());
        });
        ClickUtil.click(tvRegisterLogin, this::finish);
    }

    public void registerSuccess() {
        finish();
    }

    public void sendCodeOk() {
        startCount();
    }

    public void sendCodeError() {
    }

    public void startCount() {
        tvGetVerificationCode.setEnabled(false);
        DisposUtil.close(disposableSubscriber);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                tvGetVerificationCode.setText(String.format(App.INSTANCE.getString(R.string.app_count_down_register), time));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                tvGetVerificationCode.setText("再次获取验证码");
                tvGetVerificationCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }
            }
        };
        etVerificationCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 6) {
                    tvRegister.setEnabled(true);
                } else {
                    tvRegister.setEnabled(false);
                }
            }
        });

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DisposUtil.close(disposableSubscriber);
    }
}
